﻿using System;

class SortingAnArray
{
    static void Main()
    {
        //Exercise 7.
        //Sorting an array means to arrange its elements in increasing order. Write a program to sort an array. 
        //Use the "selection sort" algorithm: Find the smallest element, move it at the first position, find the smallest 
        //from the rest, move it at the second position, etc.


        //enter the length and the elements of the array from the console
        Console.Write("Type the length of the array (N):  ");
        int N = int.Parse(Console.ReadLine());
        Console.WriteLine("Type the elements of the array:");
        int[] array = new int[N];
        for (int i = 0; i < N; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }

        int min;                        // index of the minimal element
        for (int i = 0; i < array.Length - 1; i++)
        {
            min = i;                    //assume the first element is the smallest
            for (int j = i + 1; j < array.Length; j++)
            {
                if (array[j] < array[min])
                {
                    min = j;
                }
            }
            if (min != i)               //exchange the two element
            {
                int temp = array[i];
                array[i] = array[min];
                array[min] = temp;                    
            }
        }
        Console.WriteLine();
        for (int i = 0; i < array.Length; i++)      //print the array in increasing order
        {
            Console.Write(array[i] + " ");
        }
        Console.WriteLine();
    }
}

